/**
 * 订单相关API接口
 * @module api/order
 * @author 团队名称
 * @description 订单模块相关接口请求函数
 */
import request from './request';
import { USE_MOCK } from './config';
import { mockGetOrderList, mockGetOrderDetail, mockPayOrder, mockConfirmReceipt, mockGetLogistics } from '../mock/order';

/**
 * 获取订单列表
 * @function getOrderList
 * @param {Object} params - 查询参数
 * @param {String} [params.status] - 订单状态：全部='all'，待付款='unpaid'，待发货='undelivered'，待收货='delivered'，已完成='completed'，已取消='cancelled'
 * @param {Number} [params.page=1] - 页码
 * @param {Number} [params.pageSize=10] - 每页条数
 * @param {String} [params.startDate] - 开始日期，格式：YYYY-MM-DD
 * @param {String} [params.endDate] - 结束日期，格式：YYYY-MM-DD
 * @returns {Promise} 返回Promise对象
 */
export const getOrderList = (params) => {
  if (USE_MOCK) {
    return mockGetOrderList(params);
  }
  return request.get('/api/orders', params);
};

/**
 * 获取订单详情
 * @function getOrderDetail
 * @param {Number|String} orderId - 订单ID
 * @returns {Promise} 返回Promise对象
 */
export const getOrderDetail = (orderId) => {
  if (USE_MOCK) {
    return mockGetOrderDetail(orderId);
  }
  return request.get(`/api/orders/${orderId}`);
};

/**
 * 取消订单
 * @function cancelOrder
 * @param {Number|String} orderId - 订单ID
 * @returns {Promise} 返回Promise对象
 */
export const cancelOrder = (orderId) => {
  if (USE_MOCK) {
    // 使用mockPayOrder函数的逆操作，实际项目中应该有独立的mockCancelOrder
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 200,
          message: '取消成功',
          data: { success: true }
        });
      }, 500);
    });
  }
  return request.put(`/api/orders/${orderId}/cancel`);
};

/**
 * 支付订单
 * @function payOrder
 * @param {Number|String} orderId - 订单ID
 * @param {Object} paymentData - 支付数据
 * @param {String} paymentData.paymentMethod - 支付方式：微信支付='wechat'，支付宝='alipay'，银行卡='bank'
 * @returns {Promise} 返回Promise对象
 */
export const payOrder = (orderId, paymentData) => {
  if (USE_MOCK) {
    return mockPayOrder(orderId, paymentData);
  }
  return request.post(`/api/orders/${orderId}/pay`, paymentData);
};

/**
 * 确认收货
 * @function confirmReceipt
 * @param {Number|String} orderId - 订单ID
 * @returns {Promise} 返回Promise对象
 */
export const confirmReceipt = (orderId) => {
  if (USE_MOCK) {
    return mockConfirmReceipt(orderId);
  }
  return request.put(`/api/orders/${orderId}/confirm`);
};

/**
 * 获取物流信息
 * @function getLogistics
 * @param {Number|String} orderId - 订单ID
 * @returns {Promise} 返回Promise对象
 */
export const getLogistics = (orderId) => {
  if (USE_MOCK) {
    return mockGetLogistics(orderId);
  }
  return request.get(`/api/orders/${orderId}/logistics`);
};
